package kr.co.doriclassic.summary_java.serviceUI;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import kr.co.doriclassic.mylibrary.ConnectionFactory;
import kr.co.doriclassic.mylibrary.InputPanel;
import kr.co.doriclassic.summary_java.dao.DepartmentDao;
import kr.co.doriclassic.summary_java.dto.Department;

public class PanelDepartmentUI extends JPanel implements ActionListener {
	private JButton btnInsert;
	private JButton btnUpdate;
	private JButton btnDelete;
	private InputPanel pDeptNo;// 라이브러리 패널
	private InputPanel pDeptName;// 라이브러리 패널
	private InputPanel pFloor;// 라이브러리 패널
	private DepartmentDao dao;
	private Department dept;
	private JPanel panel_top;
	private JLabel lblNewLabel;
	private JPanel panel;
		
	//insert form
	public PanelDepartmentUI() {
		initialize();
		btnUpdate.setVisible(false);
		btnDelete.setVisible(false);
		this.setLblNewLabel("부서정보추가");
	}

	//other form
	public PanelDepartmentUI(Department dept) {
		initialize();
		this.dept = dept;
		btnInsert.setVisible(false);
		pDeptNo.setTf(dept.getDeptNo()+""); 
		pDeptNo.editableTf(false);
		pDeptName.setTf(dept.getDeptName()); 
		pFloor.setTf(dept.getFloor()+""); 
		this.setLblNewLabel("부서정보수정");
		dao=new DepartmentDao(ConnectionFactory.getConnection());
	}

	public DepartmentDao getDao() {
		return dao;
	}

	public void setDao(DepartmentDao dao) {
		this.dao = dao;
	}

	public String getLblNewLabel() {
		return lblNewLabel.getText();
	}

	public void setLblNewLabel(String s) {
		lblNewLabel.setText(s);
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		panel_top = new JPanel();
		add(panel_top, BorderLayout.NORTH);

		lblNewLabel = new JLabel("New label");
		panel_top.add(lblNewLabel);

		JPanel panel_center = new JPanel();
		add(panel_center, BorderLayout.CENTER);
		panel_center.setLayout(new GridLayout(0, 1, 0, 0));

		pDeptNo = new InputPanel("부서번호");
		panel_center.add(pDeptNo);

		pDeptName = new InputPanel("부서명");
		panel_center.add(pDeptName);

		pFloor = new InputPanel("위치");
		panel_center.add(pFloor);

		JPanel panel_bottm = new JPanel();
		add(panel_bottm, BorderLayout.SOUTH);
		panel_bottm.setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		panel_bottm.add(panel, BorderLayout.NORTH);

		btnInsert = new JButton("추가");
		btnInsert.addActionListener(this);
		panel.add(btnInsert);
				
		btnUpdate = new JButton("수정");
		btnUpdate.addActionListener(this);
		panel.add(btnUpdate);
		
		btnDelete = new JButton();
		btnDelete.setText("삭제");
		panel.add(btnDelete);
		btnDelete.addActionListener(this);
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			dept = inputInfo(true);
			try {
				dao.insertItem(dept);
				JOptionPane.showMessageDialog(this, "입력되었습니다");
				rPaint();	
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			} catch (NullPointerException e1){}
			
		} else if (e.getActionCommand().equals("수정")) {
			dept = inputInfo(true);
			try {
				dao.updateItembyNo(dept);
				JOptionPane.showMessageDialog(this, "수정되었습니다");
				rPaint();			
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			} catch (NullPointerException e1){}

		} else if (e.getActionCommand().equals("삭제")) {
			dept = inputInfo(false);
			try {
				dao.deleteItembyNo(dept);
				JOptionPane.showMessageDialog(this, "삭제되었습니다");
				rPaint();	
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			} catch (NullPointerException e1){}
		} 
	}

	private void rPaint() {
		this.removeAll();
		PanelDepartmentTable tb = new PanelDepartmentTable();
		tb.setLblNewLabel("부서정보");
		try {
			tb.setList(dao.listItems());
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
		}
		tb.setDao(dao);
		this.add(tb);
		this.revalidate();
	}

	private Department inputInfo(boolean b) {
		Department temp=null;
		try {
			int dno = Integer.parseInt(pDeptNo.getTf().trim());
			if (b) {
				String name = pDeptName.getTf().trim();
				int floor = Integer.parseInt(pFloor.getTf().trim());
				temp = new Department(dno, name, floor);
			} else {
				temp = new Department();
				temp.setDeptNo(dno);				
			}
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "숫자만 입력 가능함");
		}		
		return temp;
	}	
}
